<script setup lang="ts">
import { useSendMobileCode } from '@/composable'
import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { codeRules, mobileRules, passwordRules } from '@/utils/rules'
import { showFailToast, showSuccessToast } from 'vant'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const mobile = ref('13230000001') // 手机号
const password = ref('abc12345') // 密码
const code = ref('') // 验证码
const agree = ref(false) // 协议是否勾选
const isPass = ref(true) // 切换验证码登录
const show = ref(false) // 控制密码是否显示
const store = useUserStore() // pinia仓库
const router = useRouter()
const route = useRoute()

onMounted(() => {
  // 判断url是否有回跳地址
  if (route.query.returnUrl) {
    const store = useUserStore()
    store.setReturnUrl(route.query.returnUrl as string)
  }
})

// 登录函数
const login = async () => {
  if (!agree.value) return showFailToast('请勾选协议!')
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  store.setUser(res.data)
  // 跳转
  router.replace(store.returnUrl || '/user')
  // 删路径
  store.delUrl()
  showSuccessToast('登录成功')
}

// 发送验证码
const { form, time, send } = useSendMobileCode(mobile)

const url = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${
  import.meta.env.VITE_APP_CALLBACK
}/login/callback`
</script>

<template>
  <cp-nav-bar rightText="注册" title="登录"></cp-nav-bar>
  <div class="login-page">
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>
          {{ !isPass ? '密码登录' : '短信验证码登录' }}
        </span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form ref="form" autocomplete="off" @submit="login">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        :rules="mobileRules"
        v-model="mobile"
        name="mobile"
      />
      <van-field
        placeholder="请输入密码"
        :type="show ? 'text' : 'password'"
        :rules="passwordRules"
        v-model="password"
        v-if="isPass"
      >
        <template #button>
          <CpIcon
            :name="`login-eye-${show ? 'on' : 'off'}`"
            @click="show = !show"
          ></CpIcon>
        </template>
      </van-field>
      <van-field
        placeholder="请输入验证码"
        type="tel"
        :rules="codeRules"
        v-model="code"
        v-else
      >
        <template #button>
          <van-button size="small" type="primary" @click="send">
            {{ time ? time + 's后发送验证码' : '发送验证码' }}
          </van-button>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" nativeType="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a class="icon" :href="url">
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '../../styles/login.scss';
</style>
